<?php
/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com.cn
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */

namespace app\shop\controller;

use app\model\system\Addon;
use app\model\web\DiyView as DiyViewModel;

/**
 * 网站装修控制器
 */
class Diy extends BaseShop
{
	/**
	 * 网站主页
	 */
	public function index()
	{
		$diy_view = new DiyViewModel();
		$page = $diy_view->getPage();

		// 查询公共组件和支持的页面
		$condition = [
			[ 'support_diy_view', 'like', [ $page[ 'shop' ][ 'index' ][ 'name' ], '%' . $page[ 'shop' ][ 'index' ][ 'name' ] . ',%', '%' . $page[ 'shop' ][ 'index' ][ 'name' ], '%,' . $page[ 'shop' ][ 'index' ][ 'name' ] . ',%', 'DIY_VIEW_SHOP', '' ], 'or' ]
		];

		$data = [
			'app_module' => $this->app_module,
			'site_id' => $this->site_id,
			'name' => $page[ 'shop' ][ 'index' ][ 'name' ],
			'condition' => $condition
		];
		$edit_view = event('DiyViewEdit', $data, true);
		return $edit_view;
	}

	/**
	 * 商品分类页面
	 */
	public function goodsCategory()
	{
		$diy_view = new DiyViewModel();
		$page = $diy_view->getPage();
		// 查询公共组件和支持的页面
		$condition = [
			[ 'name', '=', 'GOODS_CATEGORY' ]
		];

		$data = [
			'app_module' => $this->app_module,
			'site_id' => $this->site_id,
			'name' => $page[ 'shop' ][ 'goods_category' ][ 'name' ],
			'condition' => $condition
		];
		$edit_view = event('DiyViewEdit', $data, true);
		return $edit_view;
	}

	/**
	 * 编辑
	 */
	public function edit()
	{
		if (request()->isAjax()) {
			$res = 0;
			$data = array ();
			$id = input("id", 0);
			$name = input("name", "");
			$title = input("title", "");
			$value = input("value", "");
			if (!empty($name) && !empty($title) && !empty($value)) {
				$diy_view = new DiyViewModel();
				$page = $diy_view->getPage();
				$data[ 'site_id' ] = $this->site_id;
				$data[ 'name' ] = $name;
				$data[ 'title' ] = $title;
				$data[ 'type' ] = $page[ 'shop' ][ 'port' ];
				$data[ 'value' ] = $value;
				if ($id == 0) {
					$data[ 'create_time' ] = time();
					$res = $diy_view->addSiteDiyView($data);
				} else {
					$data[ 'update_time' ] = time();
					$res = $diy_view->editSiteDiyView($data, [
						[ 'id', '=', $id ]
					]);
				}
			}

			return $res;
		} else {

			$id = input("id", 0);
			//查询公共系统组件
			$condition = [
				[ 'support_diy_view', 'like', [ '', 'DIY_VIEW_SHOP' ], 'or' ]
			];
			$data = [
				'app_module' => $this->app_module,
				'site_id' => $this->site_id,
				'id' => $id,
				'condition' => $condition
			];
			$edit_view = event('DiyViewEdit', $data, true);
			return $edit_view;
		}
	}

	/**
	 * 微页面
	 */
	public function lists()
	{
		if (request()->isAjax()) {
			$page_index = input('page', 1);
			$page_size = input('page_size', PAGE_LIST_ROWS);
			$diy_view = new DiyViewModel();
			$page = $diy_view->getPage();
			$condition = array (
				[ 'sdv.site_id', '=', $this->site_id ],
				[ 'sdv.type', '=', $page[ 'shop' ][ 'port' ] ],
				[ 'sdv.name', 'like', '%DIY_VIEW_RANDOM_%' ]
			);
			$list = $diy_view->getSiteDiyViewPageList($condition, $page_index, $page_size, "sdv.create_time desc");
			return $list;
		} else {
			return $this->fetch('diy/lists');
		}
	}

	/**
	 * 链接选择
	 */
	public function link()
	{
		$link = input("link", []);
		$support_diy_view = input("support_diy_view", '');//支持的自定义页面（为空表示都支持）
		$is_array = true;//记录是否是数组，后续判断受该变量影响
		if (!empty($link)) {
			$link = json_decode($link, true);
			if (empty($link[ 'name' ])) {
				$link = [];
			}
			$is_array = is_array($link);
		}

		$diy_view_model = new DiyViewModel();
		$link_condition = [
			[ 'lk.support_diy_view', 'like', [ $support_diy_view, '', 'DIY_VIEW_SHOP' ], 'or' ]
		];
		$res = $diy_view_model->getDiyLinkList($link_condition);
		$res = $res[ 'data' ];

		//查询当前站点的微页面，暂时不显示
		$page = $diy_view_model->getPage();
		$condition = [
			[ 'sdv.site_id', '=', $this->site_id ],
			[ 'sdv.type', '=', $page[ 'shop' ][ 'port' ] ],
			[ 'ndva.addon_name', '=', null ], //排除插件中的自定义模板
			[ 'sdv.name', 'not in', [ $page[ 'admin' ][ 'index' ][ 'name' ], $page[ 'admin' ][ 'goods_category' ][ 'name' ], $page[ 'shop' ][ 'index' ][ 'name' ], $page[ 'shop' ][ 'goods_category' ][ 'name' ] ], 'and' ]
		];

		$site_diy_view_list = $diy_view_model->getSiteDiyViewPageList($condition, 1, 0, "sdv.create_time desc");
		$site_diy_view_list = $site_diy_view_list[ 'data' ][ 'list' ];

		$addon_model = new Addon();
		$link_list = array ();

		// 调整链接结构，先遍历，插件分类
		foreach ($res as $k => $v) {
			$value = array ();
			if (empty($v[ 'addon_name' ])) {
				$v[ 'addon_title' ] = '系统';
			}
			$value[ 'addon_name' ] = $v[ 'addon_name' ];
			$value[ 'addon_title' ] = $v[ 'addon_title' ];
			$value[ 'icon' ] = $v[ 'addon_icon' ];
			$value[ 'list' ] = [];
			$column = array_column($link_list, 'addon_name');
			if (!in_array($v[ 'addon_name' ], $column)) {
				array_push($link_list, $value);
			}
		}

		foreach ($site_diy_view_list as $k => $v) {
			$value = array ();
			$addon_info = $addon_model->getAddonInfo([ 'name' => $v[ 'addon_name' ] ], 'title,icon');
			$addon_info = $addon_info [ 'data' ];
			$value[ 'addon_name' ] = $v[ 'addon_name' ];
			$value[ 'addon_title' ] = $addon_info[ 'title' ];
			$value[ 'icon' ] = $addon_info[ 'icon' ];
			$value[ 'list' ] = [];
			$column = array_column($link_list, 'addon_name');
			if (!in_array($v[ 'addon_name' ], $column)) {
				array_push($link_list, $value);
			}
		}

		$temp_link = [];

		// 遍历每一个链接，将其添加到对应的插件分类中
		foreach ($link_list as $diy_k => $diy_v) {

			//遍历插件固定自定义页面
			foreach ($res as $fixed_k => $fixed_v) {
				if ($diy_v[ 'addon_name' ] == $fixed_v[ 'addon_name' ]) {
					if (!empty($link) && $is_array && $link[ 'name' ] == $fixed_v[ 'name' ]) {
						//对象方式匹配
						$fixed_v[ 'selected' ] = true;
					} elseif (!empty($link) && !$is_array && strtolower($link) == strtolower($fixed_v[ 'wap_url' ])) {
						//字符串方式匹配
						$fixed_v[ 'selected' ] = true;
						$temp_link = $fixed_v;
					} else {
						$fixed_v[ 'selected' ] = false;
					}
					array_push($link_list[ $diy_k ][ 'list' ], $fixed_v);
				}
			}

			//遍历微页面
			foreach ($site_diy_view_list as $page_k => $page_v) {

				if ($diy_v[ 'addon_name' ] == $page_v[ 'addon_name' ]) {
					$title = $page_v[ 'title' ];
					$item = [
						'id' => $page_v[ 'id' ],
						'name' => $page_v[ 'name' ],
						'title' => $title,
						'addon_icon' => "",
						'addon_name' => $page_v[ 'addon_name' ],
						'addon_title' => $diy_v[ 'addon_title' ],
						'web_url' => '',
						'wap_url' => '/otherpages/diy/diy/diy?name=' . $page_v[ 'name' ],
						'icon' => '',
						'type' => 0,
						'diy' => 1
					];

					if (!empty($link) && $is_array && $link[ 'name' ] == $page_v[ 'name' ]) {
						//对象方式匹配
						$item[ 'selected' ] = true;
					} elseif (!empty($link) && !$is_array && strtolower($link) == strtolower($page_v[ 'wap_url' ])) {
						//字符串方式匹配
						$item[ 'selected' ] = true;
						$temp_link = $page_v;
					} else {
						$item[ 'selected' ] = false;
					}
					array_push($link_list[ $diy_k ][ 'list' ], $item);

				}
			}
		}

		if (!$is_array) {
			$link = $temp_link;
		}

		$this->assign("link", $link);
		$this->assign("link_list", $link_list);
		return $this->fetch('diy/link');
	}

	/**
	 * 删除自定义模板页面
	 */
	public function deleteSiteDiyView()
	{
		if (request()->isAjax()) {
			$diy_view = new DiyViewModel();
			$id_array = input("id", 0);
			$condition = [
				[ 'id', 'in', $id_array ]
			];
			$res = $diy_view->deleteSiteDiyView($condition);
			return $res;
		}
	}

	/**
	 * 底部导航
	 */
	public function bottomNavDesign()
	{
		$diy_view = new DiyViewModel();
		if (request()->isAjax()) {
			$value = input("value", "");
			$res = $diy_view->setShopBottomNavConfig($value, $this->site_id);
			return $res;
		} else {
			$bottom_nav_info = $diy_view->getShopBottomNavConfig($this->site_id);
			$this->assign("bottom_nav_info", $bottom_nav_info[ 'data' ][ 'value' ]);
			return $this->fetch('diy/bottom_nav_design');
		}

	}

	/**
	 * 推广链接
	 */
	public function promote()
	{
		if (request()->isAjax()) {
			$id = input("id", 0);
			$diy_view = new DiyViewModel();
			$res = $diy_view->qrcode([
				[ 'site_id', '=', $this->site_id ],
				[ 'id', '=', $id ]
			]);
			return $res;
		}
	}

}